package org.ykhl.appserver.handlers;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import org.ykhl.appserver.dao.PollRequest;
import org.ykhl.appserver.dao.Request;
import org.ykhl.appserver.exceptions.NotAuthorizedException;
import org.ykhl.appserver.services.RequestSendService;
import org.ykhl.mq.commons.BaseMessage;
import org.ykhl.mq.commons.ConnectionException;
import org.ykhl.mq.commons.Constants;
import org.springframework.beans.factory.annotation.Autowired;

@RestController
public class RequestHandler {
	private static final Logger LOG = LoggerFactory.getLogger(RequestHandler.class);
	@Autowired
	private RequestSendService requestSendService;

	@RequestMapping(value="/echoRequest", method= RequestMethod.POST)
	public Request echo(Request request){

		return request;
	}

	@RequestMapping(value="/request", method= RequestMethod.POST
			, produces="application/json;charset=UTF-8")
	public String processRequest(@RequestHeader("ssl-dn") String sslDn, Request request)
			throws ConnectionException {
		String authId = getAuthIdFromDn(sslDn);
		LOG.debug("AuthId :" + authId);
		request.setAuthId(authId);
		requestSendService.messageReceivedFromRequest(request);
		return requestSendService.sendRequest(request);
	}
	@RequestMapping(value="/response", produces="application/json;charset=UTF-8")
	public BaseMessage pollResponse(PollRequest pollRequest) {
		BaseMessage baseMessage = requestSendService.getOneResponse(pollRequest);
		requestSendService.messageSentToResponse(baseMessage);//添加日志记录信息
		return baseMessage;
	}

	@RequestMapping(value="/singleRequest", method= RequestMethod.POST
			, produces="application/json;charset=UTF-8")
	public BaseMessage processSingleRequest(Request request) throws ConnectionException {

		requestSendService.messageReceivedFromRequest(request);
		BaseMessage baseMessage =  requestSendService.sendSingleRequest(request);
		requestSendService.messageSentToResponse(baseMessage);//添加日志记录信息
		LOG.debug("receiver response message,fid"+ baseMessage.getFid());

		return baseMessage ;
	}

	//单条消息请求
	@RequestMapping(value="/requestMessage", method= RequestMethod.POST
			, produces="application/json;charset=UTF-8")
	public String processMessageBody(@RequestHeader("ssl-dn") String sslDn,Request request) throws ConnectionException {
		String authId = getAuthIdFromDn(sslDn);
		LOG.debug("AuthId :" + authId);
		request.setAuthId(authId);
		requestSendService.messageReceivedFromRequest(request);
		BaseMessage baseMessage =  requestSendService.sendSingleRequest(request);
		requestSendService.messageSentToResponse(baseMessage);
		return baseMessage.getMessageBody();//返回消息体
	}

	@RequestMapping(value="/", method=RequestMethod.GET)
	@ResponseBody
	public String test(){
		return "SUCCESS, path: " + Constants.CLASSPATH;
	}


	private static String getAuthIdFromDn(String dn) {
		LOG.debug("subject dn: " + dn);
		if (dn == null || dn.isEmpty()) throw new NotAuthorizedException("missing header: ssl-dn");

		int idx = dn.indexOf("appserver-");
		if (idx == -1) throw new NotAuthorizedException("wrong format of header: ssl-dn");

		return dn.substring(idx + "appserver-".length());
	}

}
